/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;


import java.util.Properties;
import javax.servlet.http.HttpServlet;
import bean.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author user
 */
public class chatHandler extends HttpServlet {
    roomListBean roomlist = new roomListBean();
    Properties properties = null;

    public void addRoom(roomListBean list, String name, String owner, String parent) {
        roomBean room = new roomBean(name, owner, parent);
        room.setChatLength(30);
        roomlist.addRoom(room);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("RoomList created");
        out.close();
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String owner = request.getParameter("owner");
        String parent = request.getParameter("parent");
        if(name == null || (name = name.trim()).length() == 0 || owner == null || (owner = owner.trim()).length() == 0) {
            request.setAttribute("error", "cant make room with empty string");
            getServletContext().getRequestDispatcher("/home.jsp");
            return;
        }
        else if (name != null && name.indexOf(" ") != -1) {
            request.setAttribute("error", "cant make room with spaces");
            getServletContext().getRequestDispatcher("/home.jsp");
            return;
        }
        try {
            if (roomlist != null) {
                addRoom(roomlist, name, owner, parent);
            }
        }
        catch (Exception e) {
            System.err.println("Exception :" + e);
        }

    }

    @Override
    public void init() throws ServletException {
    }



}
